var express = require('express');
var router = express.Router();
var retUtil = require('../../../../utils/retUtil');
var api4back = require('../../../../service/api4back');
var CONST = require('../../../../utils/const');


/**
 * 获取推广码列表
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：pageNum : 请求页码
 * 参数：pageSize : 请求页容量
 * 参数：withTotalPage : 是否需要总页数
 * 参数：cType : 检索条件：码类型（10直接抵扣,20满减券,30折扣券,40活动推广码）
 * 参数：userId : 检索条件：用户id（只返回绑定到该用户的码）
 * 参数：desc : 检索条件：码备注（模糊匹配）
 * 参数：state : 码的状态
 */
function getPromoCodeList(req, res, next) {
    //获取参数
    var pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 10;
    var withTotalPage = 1;
    var cType = retUtil.stringFromReq(req, 'cType') || "[10,20,30,40]";
    var userId = 0;
    var desc = retUtil.stringFromReq(req, 'desc') || "";
    var state = retUtil.stringFromReq(req, 'state') || "[20,40]";

    if(!desc || desc.length == 0 || desc == "null"){
        desc = "";
    }

    //参数判断
    if(req.session[CONST.SESSION_USER]
        && req.session[CONST.SESSION_USER].userInfo
        && req.session[CONST.SESSION_USER].userInfo.userId){
        userId = req.session[CONST.SESSION_USER].userInfo.userId;
    }else{
        return retUtil.ret_err_json("用户唯一表示无效",res);
    }

    //数据请求
    var para = {
        body :{
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage,
            userId:userId
        },
        method : "POST"
    };

    if(cType.length){
        para.body.cType = cType;
    }
    if(state.length){
        para.body.state = state;
    }
    if(desc.length){
        para.body.desc = desc;
    }
    api4back.getPromoCodeList(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        apiRes.pageSize = pageSize;
        if(apiRes.list.length>0){
            for(var i = 0;i<apiRes.list.length;i++){
                if(apiRes.list[i].c_type == 10){
                    apiRes.list[i].c_type_desc = "直接抵扣券";
                }else if(apiRes.list[i].c_type == 20){
                    apiRes.list[i].c_type_desc = "满减券";
                }else if(apiRes.list[i].c_type == 30){
                    apiRes.list[i].c_type_desc = "折扣券";
                }else if(apiRes.list[i].c_type == 40){
                    apiRes.list[i].c_type_desc = "活动推广码";
                }
                apiRes.list[i].key = apiRes.list[i].c_id;
            }
        }

        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getPromoCodeList', getPromoCodeList);
router.post('/getPromoCodeList', getPromoCodeList);




/**
 * 生成优惠码/推广码
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：cType : 码类型（10直接抵扣,20满减券,30折扣券,40活动推广码）
 * 参数：userId : 用户id（只返回绑定到该用户的码）
 * 参数：desc :码备注
 */
function createPromoCode(req, res, next) {
    //获取参数
    var cType = retUtil.numberFromReq(req, 'cType01') || 40;
    var userId = 0;
    var desc = retUtil.stringFromReq(req, 'desc01') || "";

    //参数判断
    //参数判断
    if(req.session[CONST.SESSION_USER] && req.session[CONST.SESSION_USER].userInfo && req.session[CONST.SESSION_USER].userInfo.userId){
        userId = req.session[CONST.SESSION_USER].userInfo.userId;
    }else{
        return retUtil.ret_err_json("用户唯一表示无效",res);
    }

    //数据请求
    var para = {
        body :{
            cType:cType,
            desc:desc,
            userId:userId
        },
        method : "POST"
    };
    api4back.createPromoCode(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/createPromoCode', createPromoCode);
router.post('/createPromoCode', createPromoCode);



/**
 * 获取推广码奖励信息列表
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：pageNum : 请求页码
 * 参数：pageSize : 请求页容量
 * 参数：withTotalPage : 是否需要总页数
 * 参数：userId : 检索标间：码绑定用户的id
 * 参数：desc : 检索条件：码备注（模糊匹配）
 * 参数：actId : 检索条件：活动id
 * 参数：cCode : 检索条件：推广码
 * 参数：cId : 检索条件：码id
 */
function getPromoCodeRewardList(req, res, next) {
    //获取参数
    var pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 10;
    var withTotalPage = 1;
    var actId = retUtil.numberFromReq(req, 'actId') || 0;
    var cCode = retUtil.stringFromReq(req, 'cCode') || "";
    var cId = retUtil.numberFromReq(req, 'cId') || 0;
    var desc = retUtil.stringFromReq(req, 'desc') || "";
    var userId = 0;

    if(!desc || desc.length == 0 || desc == "null"){
        desc = "";
    }

    if(!cCode || cCode.length == 0 || cCode == "null"){
        cCode = "";
    }

    //参数判断
    if(req.session[CONST.SESSION_USER] && req.session[CONST.SESSION_USER].userInfo && req.session[CONST.SESSION_USER].userInfo.userId){
        userId = req.session[CONST.SESSION_USER].userInfo.userId;
    }else{
        return retUtil.ret_err_json("用户唯一表示无效",res);
    }

    //数据请求
    var para = {
        body :{
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage,
            userId:userId
        },
        method : "POST"
    };

    if(actId){
        para.body.actId = actId;
    }
    if(cCode.length){
        para.body.cCode = cCode;
    }
    if(cId){
        para.body.cId = cId;
    }
    if(desc.length){
        para.body.desc = desc;
    }
    api4back.getPromoCodeRewardList(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        apiRes.pageSize = pageSize;
        if(apiRes.list.length>0){
            for(var i = 0;i<apiRes.list.length;i++){
                apiRes.list[i].key = i*1+1;
                apiRes.list[i].p_reward = (apiRes.list[i].p_reward/100).toFixed(2)+"元";
            }
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getPromoCodeRewardList', getPromoCodeRewardList);
router.post('/getPromoCodeRewardList', getPromoCodeRewardList);








module.exports = router;
